import store from '@/store';
import vm from 'vue'

// 判断是否登陆
export function jugeIsLogin() {
    // console.log(store)
    return new Promise((resolve, reject) => {
        if (store.state.vuex_userInfo.isLogin) {
            resolve();
        } else {
            reject();
        }
    });
}

export function formatData(Data) {
    let list = []
    // 遍历 学校
    Data.map(schoolItem => {
        var form1 = {
            value: schoolItem.schoolId,
            label: schoolItem.schoolName,
            address: schoolItem.schoolAddress,
            phone: schoolItem.schoolPhone,
            schoolLatitude: schoolItem.schoolLatitude,
            schoolLongitude: schoolItem.schoolLongitude,
            children: []
        }
        // 遍历 楼栋
        schoolItem.buildList.map(buildItem => {
            var form2 = {
                value: buildItem.buildId,
                label: buildItem.buildName,
                // children: []
            }
            // 遍历 宿舍
            // buildItem.roomList.map(roomItem => {
            //     var form3 = {
            //         value: roomItem.roomId,
            //         label: roomItem.roomName,
            //     }
            //     form2.children.push(form3)
            // })
            form1.children.push(form2)
        })
        list.push(form1)
    })
    return list
}


/**
 * 消息订阅
 */
export function messageDingyue() {
    // 获取用户的当前设置，判断是否点击了“总是保持以上，不在询问”
    return new Promise((resolve) => {
        uni.getSetting({
            withSubscriptions: true, //是否获取用户订阅消息的订阅状态，默认false不返回
            success(res) {
                console.log(res.subscriptionsSetting["mainSwitch"]);
                if (!res.subscriptionsSetting["mainSwitch"]) {
                    uni.openSetting({
                        // 打开设置页
                        success(res) {
                            console.log(res.authSetting);
                        },
                    });
                } else {
                    // 用户没有点击“总是保持以上，不再询问”则每次都会调起订阅消息
                    uni.requestSubscribeMessage({
                        tmplIds: [
                            "CvrpCR0hy8v_kMZ8hLM9-8CkJxwG0BwRVbqe72RVgCY", "1BehNqwMkxG1_BcE4haW9OjEziw1uFw5dz8Tz57Gazk", "zWVgtVjIJK0vurzxrC3dZOwkXlkNEbFlVo0LB75eZ8k"
                        ],
                        success(res) {
                            resolve();
                            // console.log(res)
                            // if (res["zWVgtVjIJK0vurzxrC3dZOwkXlkNEbFlVo0LB75eZ8k"] == "accept") {
                            //     // 字段就是tmplIds模板id
                            //     // ....
                            //     resolve()
                            // }else{
                            //     resolve()
                            // }
                        },
                        fail(error) {
                            console.log(error)
                        },
                        complete(res) {
                            console.log("1111")
                            console.log(res)
                        }
                    });
                }
            },
        });
    })
}